import { Component, OnInit, Input } from '@angular/core';
import { EquipmentService } from '../../equipment.service';
import { NzModalService } from 'ng-zorro-antd';
import { NotificationService, BullySubjectService } from 'app/shared/service';
import { Subject } from 'rxjs';
import { debounceTime, distinctUntilChanged, switchMap } from 'rxjs/operators';
import { MESSAGE_CHANNEL } from 'app/app.constants';
import * as moment from 'moment';
import { differenceInCalendarDays } from 'date-fns';

@Component({
    selector: 'itmp-equipment-alarm-history',
    templateUrl: './equipment-alarm-history.component.html',
    styleUrls: ['./equipment-alarm-history.component.css']
})
export class EquipmentAlarmHistoryComponent implements OnInit {
    @Input() accessButton;
    search$ = new Subject<object>();
    toolbarList = [
        // 工具条栏
        { className: 'icon-dingwei' },
        { className: 'icon-dayinfuyin-' },
        // { className: 'icon-screenfull' },
        { className: 'icon-xiazai' }
    ];
    renderHeader = [
        {
            name: 'Alarm_ID',
            key: null,
            value: 'alarmID',
            isChecked: true
        },
        {
            name: 'Equipment_ID',
            key: null,
            value: 'deviceID',
            isChecked: true
        },
        {
            name: 'Alarm_Source',
            key: null,
            value: 'alarmSource',
            isChecked: true
        },
        {
            name: 'Detection_Date',
            key: null,
            value: 'detectionTime',
            isChecked: true
        },
        {
            name: 'Ack_Date',
            key: null,
            value: 'ackTime',
            isChecked: true
        },
        {
            name: 'Alarm_Code',
            key: null,
            value: 'alarmCode',
            isChecked: true
        },
        {
            name: 'Description',
            key: null,
            value: 'description',
            isChecked: true
        },
        {
            name: 'Zone_ID',
            key: null,
            value: 'zoneID',
            isChecked: true
        }
    ];
    searchData: any;
    pagination: any;
    selectAlarm: any;
    period = [new Date(new Date().getTime() - 24 * 60 * 60 * 1000 * 365 * 5), new Date()];
    // 筛选框下拉列表
    selectionList = {
        // Alarm_Source: ['DMS', 'AID', 'CCTV', 'LPR']
        Alarm_Source: ['VMS', 'VD', 'CCTV', 'TSC']
    };
    dateArr: any;
    listOfData = [];
    zoneIdList = [];
    constructor(
        private eqt: EquipmentService,
        private msg: NotificationService,
        private modalService: NzModalService,
        private bully: BullySubjectService
    ) {}
    ngOnInit() {
        this.searchPipeSet();
        this.initData();
        // this.getData();
        // 监听header select zone操作
        this.bully.zoneListBehavior.subscribe(res => {
            this.zoneIdList = res['selectZoneList'] ? res['selectZoneList'] : [];
            this.getData();
        });
    }
    // 防抖函数注册
    searchPipeSet() {
        this.search$.pipe(debounceTime(1000), distinctUntilChanged()).subscribe((kw: any) => {
            this.pagination.currentPage = 1;
            this.getData();
        });
    }
    // 搜索(防抖)
    onSearch(name): void {
        this.search$.next({ name: this.searchData[name] });
    }
    // 初始化分页和搜索数据
    initData() {
        this.pagination = {
            totalEle: 10,
            pageSize: 10,
            currentPage: 1,
            currentSize: 10
        };
        this.searchData = {
            alarmID: null,
            deviceID: null,
            alarmSource: null,
            ackTime: {
                startTime: null,
                endTime: null
            },
            detectionTime: {
                startTime: null,
                endTime: null
            },
            alarmCode: null,
            description: null,
            zoneID: null
        };
        this.dateArr = {
            detectionTime: null,
            ackTime: null
        };
        this.listOfData = [];
    }
    getData() {
        const params = {
            condition: { ...this.searchData, zoneIdList: this.zoneIdList },
            exportType: null,
            pageNum: this.pagination.currentPage,
            pageSize: this.pagination.pageSize
        };
        this.eqt.getAlarmHis(params).subscribe(res => {
            const data = res.data;
            if (!data) {
                this.listOfData = [];
                return;
            }
            this.listOfData = data.list;
            this.pagination.currentSize = data.numberOfElements;
            this.pagination.totalEle = data.totalCount;
            this.listOfData.forEach((item, index) => {
                this.listOfData[index].clearTime = this.eqt.changeDate(item.clearTime, 1);
                this.listOfData[index].detectionTime = this.eqt.changeDate(item.detectionTime, 1);
                this.listOfData[index].ackTime = this.eqt.changeDate(item.ackTime, 1);
            });
        });
    }
    // 刷新
    refresh(): void {
        this.searchData = null;
        this.period = [new Date(new Date().getTime() - 24 * 60 * 60 * 1000 * 365 * 5), new Date()];
        this.initData();
        this.getData();
    }
    // 点击tr取值
    clickTR(i): void {
        this.listOfData.forEach((data, index) => {
            data.active = false;
            if (i === index) {
                // 点击每个tr给与样式active
                data.active = true;
                this.selectAlarm = data;
            }
        });
    }
    // 分页控件改变事件
    onPageChange(e) {
        this.pagination.currentPage = e;
        this.getData();
    }
    onSizeChange(e) {
        this.pagination.pageSize = e;
        const total = this.pagination.totalEle;
        const curPage = this.pagination.currentPage;
        this.pagination.currentPage = Math.ceil(total / e) < curPage ? Math.ceil(total / e) : curPage;
        this.getData();
    }
    // 日历框事件
    onDateOk(result: Date, type): void {
        /*
        if (this.dateArr.detectionTime) {
            const a = new Date(moment(this.dateArr.detectionTime[0]).format('YYYY-MM-DD 00:00:00')).getTime();
            const b = new Date(moment(this.dateArr.detectionTime[1]).format('YYYY-MM-DD 23:59:59')).getTime();
            this.searchData.detectionTime = {
                startTime: this.eqt.changeDate(this.dateArr.detectionTime && a, 0),
                endTime: this.eqt.changeDate(this.dateArr.detectionTime && b, 0)
            };
            this.onSearch('detectionTime');
        }
        if (this.dateArr.ackTime) {
            const c = new Date(moment(this.dateArr.ackTime[0]).format('YYYY-MM-DD 00:00:00')).getTime();
            const d = new Date(moment(this.dateArr.ackTime[1]).format('YYYY-MM-DD 23:59:59')).getTime();
            this.searchData.ackTime = {
                startTime: this.eqt.changeDate(this.dateArr.ackTime && c, 0),
                endTime: this.eqt.changeDate(this.dateArr.ackTime && d, 0)
            };
            this.onSearch('ackTime');
        }*/
        this.searchData[type] = {
            startTime: result[0]
                ? moment(result[0])
                      .startOf('day')
                      .valueOf()
                : null,
            endTime: result[1]
                ? moment(result[1])
                      .endOf('day')
                      .valueOf()
                : null
        };
        this.onSearch(type);
    }
    disabledDate = (current: Date): boolean => {
        // Can not select days before today and today
        return differenceInCalendarDays(current, new Date()) > 0;
    };
}
